import Action from "../../../api/Action";

// pages/action/actionForm/actionForm.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentAction: {},
    userOrder: [{
      actionTicketId: '',
      username: '',
      identity: '',
      certificate: '',
      show: false
    }, {
      actionTicketId: '',
      identity: '',
      username: '',
      certificate: '',
      show: false
    }],
    columns: ['居民身份证',
      '护照',
      '港澳居民来往内地通行证',
      '台湾居民来往大陆通行证',
      '军官证',
      '警官证',
      '士兵证',
      '其他证件'
    ],
  },
  onConfirm(event) {
    const {
      picker,
      value,
      index
    } = event.detail;
    const Index = event.currentTarget.dataset.index;
    const key = `userOrder[${Index}].certificate`;
    const showKey = `userOrder[${Index}].show`;
    this.setData({
      [key]: value,
      [showKey]: false,
      show: false
    })
  },

  onCancel(event) {
    const index = event.currentTarget.dataset.index;
    const showKey = `userOrder[${index}].show`;
    this.setData({
      [showKey]: false,
      show: false
    })
  },

  showPopup(event) {
    const index = event.currentTarget.dataset.index;
    const showKey = `userOrder[${index}].show`;
    this.setData({
      [showKey]: true,
      show: true
    });
  },

  onClose(event) {
    const index = event.currentTarget.dataset.index;
    const showKey = `userOrder[${index}].show`;
    this.setData({
      [showKey]: false,
      show: false
    });
  },


  // 更新姓名
  onNameChange(e) {
    const index = e.currentTarget.dataset.index;
    const value = e.detail;
    const key = `userOrder[${index}].username`;
    this.setData({
      [key]: value
    });
  },
  //更新身份证
  onIdentityChange(e) {
    const index = e.currentTarget.dataset.index;
    const value = e.detail;
    const key = `userOrder[${index}].identity`;
    this.setData({
      [key]: value
    });
  },

  //提交订单
  async submitOrder() {
    let ActionOrder = {
      id:'',
      actionTicketId: this.data.currentAction.id,
      relativeIdentity: this.data.userOrder[0].identity,
      relativeName: this.data.userOrder[0].username,
      childrenIdentity: this.data.userOrder[1].identity,
      childrenName: this.data.userOrder[1].username,
      status: 0,
      price:this.data.currentAction.acTicketPrice,
      createTime:new Date().toISOString()
    }
    await Action.siginUpAction(ActionOrder).then(res=>{
      ActionOrder.id=res.data
      console.log(res.data)
    }).catch(err=>{
      console.log(err)
    })
    wx.setStorage({
      key: 'tempReservationData',
      data: this.data.currentAction,
    })
    wx.setStorage({
      key: 'tempUserOrderData',
      data: ActionOrder,
    })
    wx.navigateTo({
      url: '/pages/action/actionOrder/actionOrder'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      currentAction: wx.getStorageSync('tempReservationData')
    })
    // 使用后立即清理
    wx.removeStorageSync('tempReservationData');
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})